
(function ($) {

	var fnName = 'magnifier';
	var magnifier = {
		magnifier: ".magnifier", //最外层的大容器

		container: ".magnifier-container", //选择当前主图的承载容器

		containerImg: '.images-cover', //图片的容器

		view: ".magnifier-view", //图片放大后承载容器

		width: 400, //图片放大后承载容器宽度

		height: 400, //图片放大后承载容器高度

		moveView: ".move-view", //跟随鼠标移动的容器

		moveWidth: null, //如果设置了移动盒子的宽度，则不计算缩放比例

		zoom: 4, //缩放比例

		thumbnail: ".magnifier-line > ul", //缩略图容器

		assembly: ".magnifier-btn", //按钮组

		index: 0 //当前图片的索引
	};


	window[fnName] = function (magnifierAttr) {

		//设置属性值
		if (typeof (magnifierAttr) == "object") {

			for (var n in magnifierAttr) {

				magnifier[n] = magnifierAttr[n];
			}
		}

		var _this = this;
		//绑定容器
		_this.magnifier = $(magnifier.magnifier);
		_this.container = _this.magnifier.find(magnifier.container);
		_this.view = _this.magnifier.find(magnifier.view);
		_this.moveView = _this.magnifier.find(magnifier.moveView);
		_this.thumbnail = _this.magnifier.find(magnifier.thumbnail);
		_this.assembly = _this.magnifier.find(magnifier.assembly);
		_this.containerImg = _this.magnifier.find(magnifier.containerImg);
		var imgBox = _this.containerImg;

		//設置寬高
		_this.magnifier.css({
			"width": magnifier.width
		});
		_this.container.css({
			"width": magnifier.width,
			"height": magnifier.height
		});
		_this.view.css({
			"width": magnifier.width,
			"height": magnifier.height
		});

		var boxMoveViewWidth, boxMoveViewHeight;
		if (magnifier.moveWidth) {

			boxMoveViewWidth = magnifier.moveWidth;
		} else {

			boxMoveViewWidth = magnifier.width / magnifier.zoom;
		}
		boxMoveViewHeight = boxMoveViewWidth;

		_this.moveView.css({
			"width": boxMoveViewWidth,
			"height": boxMoveViewHeight
		});

		//计算体积碰撞的变量
		var deviationXl,
			deviationXr,
			deviationYt,
			deviationYb,
			imgWidth,
			imgHieght,
			multiple;

		_this.eqImg = function () {

			var img = new Image(),
				src = _this.thumbnail.find("img").eq(magnifier.index).attr('src');

			img.src = src;

			//承载容器的宽高
			containerWidth = magnifier.width;
			containerHeight = magnifier.height;

			_this.thumbnail.find('>*').removeClass('active').eq(magnifier.index).addClass('active');

			function imgLoadEnd() {

				if (img.width == 0) {

					img.onload = imgLoadEnd;
				}

				var styleCss;
				if (img.width > img.height) {

					imgWidth = magnifier.width;
					imgHieght = img.height / (img.width / magnifier.width);
					styleCss = "top:50%;margin-top:" + (-imgHieght / 2) + "px";
				} else {

					imgWidth = img.width / (img.height / magnifier.width);
					imgHieght = magnifier.width;

					styleCss = "left:50%;margin-left:" + (-imgWidth / 2) + "px";
				}

				imgBox.empty().append('<img src="' + src + '" width="' + imgWidth + '" height="' + imgHieght + '" style="' + styleCss + '" />');

				//重新计算移动盒子与图片的倍数
				multiple = magnifier.width / boxMoveViewWidth;

				//容器加载图片
				_this.view.empty().append('<img src="' + src + '" width="' + imgWidth * multiple + '" height="' + imgHieght * multiple + '" />');

				//偏移量
				deviationXl = (magnifier.width - imgWidth) / 2;
				deviationXr = magnifier.width - deviationXl - boxMoveViewWidth + 1; //这里额外+1的是要计算容器的左边框
				deviationYt = (magnifier.height - imgHieght) / 2;
				deviationYb = magnifier.height - deviationYt - boxMoveViewHeight + 1; //这里额外+1的是要计算容器的上边框

			}

			imgLoadEnd();
		}
		//完成后执行
		_this.eqImg();

		_this.moveFn = function (e) {

			var X = (e.clientX - _this.magnifier.offset().left) - boxMoveViewWidth / 2,
				Y = (e.clientY - _this.magnifier.offset().top + $(document).scrollTop()) - boxMoveViewHeight / 2;

			endX = (X > deviationXl) ? (X < deviationXr) ? X : deviationXr : deviationXl;
			endY = (Y > deviationYt) ? (Y < deviationYb) ? Y : deviationYb : deviationYt;

			//当Y轴超出容器
			endY = (endY > 0) ? (endY > (magnifier.width - boxMoveViewHeight)) ? (magnifier.height - boxMoveViewHeight) : endY : 0;
			_this.moveView.css({
				'left': endX,
				'top': endY,
				'display': "block"
			});

			positionX = (endX - (magnifier.width - imgWidth) / 2) * multiple;
			positionY = (endY - (magnifier.height - imgHieght) / 2) * multiple;

			_this.view.css({
				'display': "block"
			}).find('img').css({
				'margin-left': -positionX,
				'margin-top': -positionY
			});
		}

		_this.container.on('mousemove', function (e) {

			_this.moveFn(e);

		}).on('mouseleave', function () {

			_this.moveView.hide();
			_this.view.hide();
		});

		var thumbnailImg = _this.thumbnail.find('>*'),
			lineLenght = thumbnailImg.length;
		_this.imgMove = function (_boole) {

			(_boole) ? magnifier.index++ : magnifier.index--;

			var _deviation = Math.ceil(magnifier.width / thumbnailImg.width() / 2);
			if (lineLenght < _deviation) {
				return false;
			}

			(magnifier.index < 0) ? magnifier.index = 0 : (magnifier.index > lineLenght - _deviation) ? magnifier.index = lineLenght - _deviation : magnifier.index;

			var endLeft = (thumbnailImg.width() * magnifier.index) - thumbnailImg.width();

			_this.thumbnail.css({

				"left": ((endLeft > 0) ? -endLeft : 0) + "px"
			});
		}

		//按钮组动作
		_this.assembly.find(">*").on('click', function () {

			_this.imgMove($(this).index());
		});

		thumbnailImg.on('click', function () {

			magnifier.index = $(this).index();

			//显示图片
			_this.eqImg();

			//缩略图位置移动
			_this.imgMove(magnifier.index);
		});

		_this.setIndex = function (n) {

			magnifier.index = (n) ? n : 0;
		}

		return _this;
	}
})(jQuery);



var user = localStorage.getItem("user")
if (user) {
	$(".user").html(user)
	$(".login").remove()
	$(".register").remove()
	$(".logininfo").html('<a href="#" class="login-out">退出</a>')

}
$(".logininfo").on("click", ".login-out", function () {
	localStorage.removeItem("user")
	window.location.reload();
})

//商品详情目录导航
var top1 = $(".menu-box").offset().top;

$(window).scroll(function () {

	var win_top = $(this).scrollTop();

	var top = $(".menu-box").offset().top;



	if (win_top >= top) {

		$(".menu-box").addClass("stick-box");

	}

	if (win_top + 60 < top1) {

		$(".menu-box").removeClass("stick-box");

	}

})





$(function () {
	PageScroll(".items-box ", ".div-item", 60)
})

function PageScroll(navclick, maodian, elseHeight) {

	$(navclick).on("click", function () {
		var scrollT = $(maodian).eq($(this).index()).offset().top;
		$(window).scrollTop(scrollT - 60)
	})

	$(window).scroll(function () {
		var con_scroll = $(this).scrollTop();
		$(maodian).each(function (index, domEle) {
			var md_index = $(this).offset().top - elseHeight;
			if (con_scroll > md_index - elseHeight) {
				$(navclick).eq(index).addClass("item-on").siblings().removeClass('item-on')
			}
		})
	});
}

//鼠标移动到保障说明
$('.service-qa-box').hover(function () {
	$('.qa-detail.hide').css('display', 'block', '!important')
}, function () {
	$('.qa-detail.hide').css('display', 'none', '!important')
})

//鼠标移动到随租随还
$('.fixed-repay').hover(function () {
	$('.fixed-repay-detail').css('display', 'block')
}, function () {
	$('.fixed-repay-detail').css('display', 'none')
})

// 

//选择配置点击事件
$('.config-tag').click(function () {
	$(this).addClass('checked').siblings().removeClass('checked')
})


//获取数据
var id = getUrlParam("id")
console.log(id)
var t_id = getUrlParam("t_id")
console.log(t_id)

function getUrlParam(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if (r != null) {
		return decodeURI(r[2]);
	}
	return null;
}
if (id != null) {
	$.ajax({
		url: "http://localhost/yidianzu/API/detail.php",
		dataType: "json",
		data: {
			id: id
		},
		success: function (data) {
			console.log(data)

			var title = `<p class="shop_title" data-img="${data.img_url}">${data.t_desc}</p><span class="condition-tip">非全新设备</span>`
			$(".right-panel h3").html(title)
			var price = `${data.price}`
			price = price.slice(1, -2)
			$("#price").html(price)
			$("#sumPrice").html(price)
			$('.rent-months-price').html('￥' + price)

			var oldValue = parseInt($('#buyCount').val());
			var TotalRent = parseInt($('#sumPrice').html())
			var TotalRent2 = parseInt(price)
			var TotalDeposit = parseInt($('#sumDepositPrice').html())
			var EquipmentPrice = parseInt($('.deposit-amount').html())
			$(document).ready(function () {
				$("#addCount").click(function () {
					EquipmentPrice += 1600;
					TotalDeposit += 1600;
					TotalRent += TotalRent2;
					oldValue++;
					$('.deposit-amount').html(EquipmentPrice)
					$('#sumDepositPrice').html(TotalDeposit);
					$('#sumPrice').html(TotalRent);
					$('#buyCount').val(oldValue);
					$("#reduceCount").css({
						'color': '#333',
						'background': '#fff',
						'cursor': 'pointer'
					})
				});
				$("#reduceCount").click(function () {
					if (oldValue == 1) {
						$("#reduceCount").css({
							'color': '#e0e0e0',
							'background': '#eee',
							'cursor': 'not-allowed'
						})
					} else {

						EquipmentPrice -= 1600;
						TotalDeposit -= 1600;
						TotalRent -= TotalRent2;
						oldValue--;
						$('.deposit-amount').html(EquipmentPrice)
						$('#sumDepositPrice').html(TotalDeposit);
						$('#sumPrice').html(TotalRent);
						$('#buyCount').val(oldValue);
					}

				});
			});

		}
	})
} else {
	$.ajax({
		url: "http://localhost/yidianzu/API/detail2.php",
		dataType: "json",
		data: {
			t_id: t_id
		},
		success: function (data) {
			console.log(data)

			var title = `<p class="shop_title" data-img="${data.img_url}">${data.title}</p><span class="condition-tip">非全新设备</span>`
			$(".right-panel h3").html(title)
			var price = `${data.price}`
			price = parseInt(price.slice(1, -2))
			$("#price").html(price)
			$("#sumPrice").html(price)
			var oldValue = parseInt($('#buyCount').val());
			var TotalRent = parseInt($('#sumPrice').html())
			var TotalRent2 = parseInt(price)
			console.log(TotalRent2)
			var TotalDeposit = parseInt($('#sumDepositPrice').html())
			var EquipmentPrice = parseInt($('.deposit-amount').html())
			$(document).ready(function () {
				$("#addCount").click(function () {
					EquipmentPrice += 1600;
					TotalDeposit += 1600;
					TotalRent += TotalRent2;
					oldValue++;
					$('.deposit-amount').html(EquipmentPrice)
					$('#sumDepositPrice').html(TotalDeposit);
					$('#sumPrice').html(TotalRent);
					$('#buyCount').val(oldValue);
					$("#reduceCount").css({
						'color': '#333',
						'background': '#fff',
						'cursor': 'pointer'
					})
				});
				$("#reduceCount").click(function () {
					if (oldValue == 1) {
						$("#reduceCount").css({
							'color': '#e0e0e0',
							'background': '#eee',
							'cursor': 'not-allowed'
						})
					} else {

						EquipmentPrice -= 1600;
						TotalDeposit -= 1600;
						TotalRent -= TotalRent2;
						oldValue--;
						$('.deposit-amount').html(EquipmentPrice)
						$('#sumDepositPrice').html(TotalDeposit);
						$('#sumPrice').html(TotalRent);
						$('#buyCount').val(oldValue);
					}

				});
			});

		}
	})
}


$(".add-to-cart-btn").click(function () {
	var user = localStorage.getItem("user")
	var title = $(".shop_title").html()
	var price = $("#price").html()
	var img_url = $(".shop_title").data("img")
	console.log(img_url)
	// var img_url = $("#img")
	if (user) {
		$.ajax({
			url: "http://localhost/yidianzu/API/shopcar.php",

			data: { user_id: user, title: title, price: price,img_url:img_url },
			success: function (data) {
				console.log(data)
				$('.yzerro').fadeIn(300);
			}
		})


	} else {
		window.location.href = "./login.html"
	}

})
//关闭模态框
$('.close').click(function () {
	closeM();
});
$('.cancel-btn').click(function () {
	closeM();
});
function closeM() {
	$('.yzerro').fadeOut(300);
}
$('.ok-btn').click(function () {


	window.location.href = "./shopcar.html"

})











